package com.example.serviceB.controller;

import com.example.serviceB.api.ServiceBAccountApi;
import com.example.serviceB.req.TransferInReq;
import com.example.serviceB.service.IAccountService;
import org.bytesoft.compensable.Compensable;
import org.bytesoft.compensable.CompensableCancel;
import org.bytesoft.compensable.CompensableConfirm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Compensable(interfaceClass = ServiceBAccountApi.class, simplified = true)
@RestController
@RequestMapping("/account")
public class ServiceBAccountController implements ServiceBAccountApi {

    @Autowired
    private IAccountService accountService;

    @Override
    @Transactional
    @PostMapping("/transfer-in")
    public void transferIn(@RequestBody TransferInReq req) {
        accountService.transferIn(req);
        throw new RuntimeException();
    }

    @CompensableConfirm
    @Transactional
    public void confirmTransferIn(@RequestBody TransferInReq req) {
        accountService.confirmTransferIn(req);
    }

    @CompensableCancel
    @Transactional
    public void cancelTransferIn(@RequestBody TransferInReq req) {
        accountService.cancelTransferIn(req);
    }

}
